﻿/****************************** Module Header ******************************\
 * Module Name:  ExMethodForCheckBoxList.cs
 * Module Version:  1.0.0 (2012-1-18)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension webcontrol instance's method.  It supplies 
 * public static extension methods to load data or get othor control.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace UCF.Core.Web
{
    /// <summary>
    /// CheckBoxList服务器控件扩展方法
    /// </summary>
    public static class ExMethodForCheckBoxList
    {
        /// <summary>
        /// 获取复选框列表控件中所有选中的值
        /// </summary>
        /// <param name="cblList"></param>
        /// <returns></returns>
        public static string GetSelectedValues(this CheckBoxList cblList)
        {
            string sOut = "";
            foreach (ListItem li in cblList.Items)
            {
                if (li.Selected) sOut += li.Value + ",";
            }
            return sOut.TrimEnd(',');
        }
        /// <summary>
        /// 勾选值列表中的选项
        /// </summary>
        /// <param name="cblList"></param>
        /// <param name="values"></param>
        public static void ItemsChecked(this CheckBoxList cblList, string[] values)
        {
            if (values == null) return;
            ListItem li;
            foreach (string s in values)
            {
                li=cblList.Items.FindByValue(s.Trim());
                if (li != null) li.Selected = true;
            }
        }
    }
}
